package com.xjoyt.pay.master.repository;

import java.util.Date;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;

import com.xjoyt.pay.master.dao.OrderRepositoryCustom;
import com.xjoyt.pay.master.po.Order;
import com.xjoyt.pay.repository.Repository;

public interface OrderRepository extends Repository<Order>, OrderRepositoryCustom {

	@Query(" select a from Order a where a.merchant.id = ?1 and a.outTradeNo = ?2 ")
	Page<Order> findAllByMerchantIdAndOutTradeNo(String merchantId, String outTradeNo, Pageable pageable);

	Page<Order> findAllByNo(String no, Pageable pageable);

	Page<Order> findAllByOutTradeNo(String outTradeNo, Pageable pageable);
	
	@Query(" select a from Order a where a.no = ?1 ")
	Order findAllByNo(String no);

	@Query(" select a.merchantNo , sum(a.money), count(*) from Order a where a.merchantNo in ?1 and a.addedDate >= ?2 and a.addedDate <= ?3 group by a.merchantNo ")
	List<Object[]> sumByMerchantNoInGroupByMerchantNoIn(List<String> merchantNos, Date startDate, Date endDate);

	@Query(" select a.merchantNo , sum(a.money), count(*) from Order a where a.merchantNo in ?1 group by a.merchantNo ")
	List<Object[]> sumByMerchantNoInGroupByMerchantNoIn(List<String> merchantNos);

	// @Query(" select a.id, sum(a.money), count(*) from Order a where a.id in
	// ?1 and a.addedDate >= ?2 and a.addedDate <= ?3 group by a.id")
	// List<Object[]> sumByOrderIdInGroupByOrderIdIn(List<String> orderIds, Date
	// startDate, Date endDate);
	//
	// @Query(" select a.id, sum(a.money), count(*) from Order a where a.id in
	// ?1 group by a.id")
	// List<Object[]> sumByOrderIdInGroupByOrderIdIn(List<String> orderIds);
}
